<?php
class MiscUtils {
  public static function dump($obj, $what = NULL) {
    if (defined ('DEBUG_ENABLED') && ! DEBUG_ENABLED) {
      return;
    }
    echo "<pre>\n";
    if (is_null ($obj)) {
      echo ($what !== NULL) ? "$what=!NULL!" : '!NULL!';
    } else if ($obj === '') {
      echo ($what !== NULL) ? "$what=!EMPTY!" : '!EMPTY!';
    } else if ($obj === false) {
      echo ($what !== NULL) ? "$what=!FALSE!" : '!FALSE!';
    } else {
      if ($what !== NULL) {
        echo "$what=<br>";
      }
      echo htmlspecialchars (print_r ($obj, true));
    }
    echo "</pre>\n";
  }

  public static function getParam($itm, $default = '', $trimText = false) {
    if (isset($_POST[$itm])) {
      return ($trimText) ? trim($_POST[$itm]) : $_POST[$itm];
    } else if (isset($_GET[$itm])) {
      return ($trimText) ? trim($_GET[$itm]) : $_GET[$itm];
    } else {
      return $default;
    }
  }

  public static function digits($value, $size) {
    $prefix = '';
    for ($i = 0, $il = $size - strlen($value); $i < $il; $i++) {
      $prefix .= '0';
    }
    return $prefix . $value;
  }

  public static function unique() {
    $unique = 'u' . number_format((mktime() * rand()), 0, '.', '');
    return substr($unique, 0, 16);
  }

  public static function createMenuInst($arr, $node) {
    if (!isset($node) || $node == NULL) {
      $node = new stdClass();
      $node->id = 0;
      $node->name = 'ROOT';
      $node->c = array();
      MiscUtils::createMenuInst($arr, $node);
      return $node;
    }

    for ($i = 0, $il = count($arr); $i < $il; $i++) {
      $tmp = $arr[$i];
      if ($tmp->parent_id == $node->id) {
        $n = $tmp;
        $n->c = array();
        $node->c[] = $n;
        MiscUtils::createMenuInst($arr, $n);
      }
    }
  }

  public static function rrmdir($dir) {
    if (is_dir($dir)) {
      $objects = scandir($dir);
      foreach ($objects as $object) {
        if ($object != "." && $object != "..") {
          if (filetype($dir . "/". $object) == "dir") {
            MiscUtils::rrmdir($dir . "/". $object);
          } else {
            unlink($dir . "/" . $object);
          }
        }
      }
      reset($objects);
      rmdir($dir);
    }
  }
  
  public static function formatDate($str, $lang, $noTime=true) {
    if ($str == '0000-00-00 00:00:00') {
      return '-';
    }
    
    $tmp1 =  split(" ", $str);
    $tmp2 = split("-", $tmp1[0]);
    $tmp3 = split(":", $tmp1[1]);
    
    $year = $tmp2[0];
    $month = $tmp2[1];
    $day = $tmp2[2];
    $hour = $tmp3[0];
    $minute = $tmp3[1];
    $second = $tmp3[2];
    
    if ($lang == 'zh_CN' || $lang == 'zh_TW') {
      if (!$noTime) {
        return $tmp1[0] . ' ' . $hour . ':' . $minute;
      } else {
        return $tmp1[0];
      }
    } else if ($lang == 'en') {
      if (!$noTime) {
        return $day . ' ' . MiscUtils::getMonth($month) . ' ' . substr($year, 2) . ', ' . $hour . ':' . $minute;
      } else {
        return $day . ' ' . MiscUtils::getMonth($month) . ' ' . substr($year, 2);
      }
    }
    return $lang;
  }
  
  public static function getMonths() {
    return array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
  }
  
  public static function getLetters() {
    return array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
  }
  
  public static function getMonth($n) {
    $m = MiscUtils::getMonths();
    return $m[$n - 1];
  }
}
?>